<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:html="http://www.w3.org/1999/xhtml"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:v="livcos.org/ns/view"
  exclude-result-prefixes="html xs f v">
	
	<xsl:import href="content.xsl"/>
	<xsl:import href="../style/web.xsl"/>
	
	<xsl:param name="ref"/>
	<xsl:param name="rev"/>
	<xsl:param name="pageViewObjId" select="f:extract-object-id(resolve-uri('../../page/doc'))"/>
	<xsl:param name="incl-toc" select="'1'"/>
	<xsl:param name="incl-tools" select="'1'"/>
	<xsl:param name="incl-editor" select="if ($rev) then '0' else '1'"/>
	<xsl:param name="incl-chapter_numbering" select="'1'"/>
	
	<xsl:variable name="contentBaseUris" select="resolve-uri('ns', $taskDefId), resolve-uri('ns')"/>
	<xsl:variable name="mainCss" select="resolve-uri('css/styles.css')"/>

	<xsl:variable name="dataObjId" select="f:find-data-object($ref)"/>
	
	<xsl:template match="/">
		<xsl:choose>
			<xsl:when test="not($ref)">
				<xsl:variable name="prep">
					<xsl:variable name="nsViews" select="f:find-objs-for-namespaces(*, $contentBaseUris, 'view')"/>
					<xsl:sequence select="f:transform('', *, (), (resolve-uri('ns/default'), $nsViews))"/>
				</xsl:variable>
				<xsl:apply-templates select="$prep/*" mode="root"/>
			</xsl:when>
			<xsl:when test="$dataObjId != ''">
				<xsl:variable name="prep">
					<xsl:variable name="obj" select="doc($dataObjId)/*"/>
					<xsl:variable name="nsViews" select="f:find-objs-for-namespaces($obj, $contentBaseUris, 'view')"/>
					<xsl:sequence select="f:transform('', $obj, (), (resolve-uri('ns/default'), $nsViews))"/>
				</xsl:variable>
				<xsl:apply-templates select="$prep/*" mode="root"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:sequence select="doc(concat('/livcos.org/web/sys/feedback/ObjectNotFound?objUri=', $ref))/*"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="/html:html" mode="root">
		<xsl:copy>
			<xsl:apply-templates select="@*"/>
			<head>
				<title><xsl:value-of select="v:head/v:title"/></title>
				<meta name="to-root-path" content="{$toRootPath}"/>
				<xsl:sequence select="html:head/html:meta"/>
				<link href="{f:caller-uri($mainCss)}" rel="stylesheet" type="text/css" />
				<link href="{f:caller-uri(resolve-uri('../edit/css/styles.css'))}" rel="stylesheet" type="text/css" />
				<xsl:sequence select="html:head/html:link"/>
				<xsl:for-each select="distinct-values(html:body//v:style/@ref)">
					<link href="{f:caller-uri(.)}" rel="stylesheet" type="text/css" />
				</xsl:for-each>
				<script type="text/javascript" src="{f:caller-uri(resolve-uri('script/scripts.js'))}">
					<xsl:comment> </xsl:comment></script>
				<xsl:if test="$incl-editor = '1'">
					<script type="text/javascript" src="{f:caller-uri(resolve-uri('../edit/script/scripts.js'))}">
						<xsl:comment> </xsl:comment></script>
				</xsl:if>
				<xsl:sequence select="html:head/html:script"/>
				<xsl:call-template name="head_ext"/>
			</head>
			<xsl:apply-templates select="html:body"/>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="/html:html/html:body">
		<body>
			<div class="contentPanel">
				<xsl:copy-of select="@id"/>
				<div class="content">
					<div class="header">
						<xsl:apply-templates select="../v:head/v:context"/>
						<xsl:apply-templates select="../v:head/v:title"/>
					</div>
					<xsl:apply-templates select="*"/>
					<xsl:if test="$incl-tools = '1'">
						<p>&#160;</p>
						<xsl:call-template name="tools"/>
					</xsl:if>
				</div>
			</div>
		</body>
	</xsl:template>
	
	<xsl:template match="v:toc">
		<xsl:if test="$incl-toc = '1'">
			<xsl:variable name="content">
				<xsl:apply-templates select="/html:html/html:body/*" mode="toc"/>
			</xsl:variable>
			<xsl:if test="$content/*">
				<div class="toc">
					<xsl:sequence select="$content/*"/>
				</div>
			</xsl:if>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="*" mode="toc">
		<xsl:apply-templates select="*" mode="toc"/>
	</xsl:template>
	
	<xsl:template match="v:chapter" mode="toc">
		<p id="{@id}~1" onclick="if (!Doc().inEditMode) location.hash = '#{@id}'">
			<xsl:if test="$incl-chapter_numbering = '1'">
				<xsl:number level="multiple" count="v:chapter" format="1.1"/>
				<xsl:text>&#160;</xsl:text>
			</xsl:if>
			<xsl:value-of select="v:title"/>
		</p>
		<xsl:apply-templates select="*" mode="toc"/>
	</xsl:template>
	
	<xsl:template name="tools">
		<div class="tools">
			<a href="{f:caller-uri(concat($pageViewObjId, $dataObjId, '.pdf'))}">
				<img src="{f:caller-uri('/livcos.org/web/img/pdf.png')}" border="0" alt="View and print in PDF"/>
			</a>
		</div>
	</xsl:template>
	
	<xsl:template name="head_ext">
	</xsl:template>
	
</xsl:stylesheet>
